package com.jiaquyun.jcyx.main.cart

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.entity.OrderDetails
import com.jiaquyun.jcyx.entity.OrderGood
import com.jiaquyun.jcyx.jetpack.mode.OrderViewModel
import com.module.lemlin.LoadingDialog
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.image.loadCenterImage
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_order_details.*
import java.lang.StringBuilder

class OrderDetailsActivity : OwnerViewModeActivity<OrderViewModel>() {

    companion object {
        private const val KEY_ORDER_DETAILS = "KEY_ORDER_DETAILS"
        fun open(orderDetails: OrderDetails) {
            val bundle = Bundle().apply {
                putSerializable(KEY_ORDER_DETAILS, orderDetails)
            }
            ActivityUtils.startActivity(bundle, OrderDetailsActivity::class.java)
        }
    }

    private val mOrderDetails: OrderDetails? by lazy {
        intent?.extras?.getSerializable(KEY_ORDER_DETAILS) as? OrderDetails
    }

    override fun viewModel(): OrderViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_order_details

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.white, view = flOrderDetailsBar)
    }

    override fun initView() {

        ivAppBack.setOnClickListener { finish() }

        mOrderDetails?.let {
            fillData(it)
        }

        rvOrderDetailsCancel.setOnClickListener {
            mOrderDetails?.let {
                viewModel.orderCancel(it.id.toString())
            }
        }
        rvOrderDetailsPayment.setOnClickListener {
            mOrderDetails?.let {
                OrderPayActivity.open(it)
            }
        }
        rvOrderDetailsConfirm.setOnClickListener {
            mOrderDetails?.let {
                viewModel.orderConfirm(it.id.toString())
            }
        }
    }

    override fun initViewMode() {

        viewModel.orderDetailsLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.START) {
                LoadingDialog.show(this, R.layout.dialog_load_style_1)
            } else if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
                LoadingDialog.dismiss()
            } else {
                LoadingDialog.dismiss()
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data ?: return@observe
            fillData(data)
        })

        viewModel.orderCancelLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.START) {
                LoadingDialog.show(this, R.layout.dialog_load_style_1)
            } else if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
                LoadingDialog.dismiss()
            } else {
                LoadingDialog.dismiss()
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            mOrderDetails?.let {
                viewModel.orderDetail(it.id.toString())
            }
        })

        viewModel.orderConfirmLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.START) {
                LoadingDialog.show(this, R.layout.dialog_load_style_1)
            } else if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
                LoadingDialog.dismiss()
            } else {
                LoadingDialog.dismiss()
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            mOrderDetails?.let {
                viewModel.orderDetail(it.id.toString())
            }
        })
    }

    private val mGoodsAdapter =
        object : BaseQuickAdapter<OrderGood, BaseViewHolder>(R.layout.item_goods_settlement) {
            override fun convert(holder: BaseViewHolder, item: OrderGood) {
                holder.setText(R.id.tvGoodsSettlementName, item.goods_name)
                    .setText(
                        R.id.tvGoodsSettlementPrice,
                        StringBuilder().append("￥").append(item.member_goods_price)
                    )
                    .setText(
                        R.id.tvGoodsSettlementCount,
                        StringBuilder().append("x").append(item.goods_num)
                    )
                    .getView<ImageView>(R.id.ivGoodsSettlementIcon)
                    .loadCenterImage(item.image)

                holder.getView<View>(R.id.view1).visibility = View.INVISIBLE
            }
        }

    private fun fillData(orderDetails: OrderDetails) {

        tvOrderDetailsStatus.text =
            if (orderDetails.order_status == "1" && orderDetails.pay_status == "0")
                "订单待付款"
            else if (orderDetails.order_status == "1" && orderDetails.pay_status == "1")
                "订单待发货"
            else if (orderDetails.order_status == "2" && orderDetails.pay_status == "1")
                "订单待收货"
            else if (orderDetails.order_status == "3" && orderDetails.pay_status == "1" && orderDetails.shipping_status != "0")
                "订单已完成"
            else "订单已取消"
        if (orderDetails.order_status == "1" && orderDetails.pay_status == "0") {
            rvOrderDetailsCancel.visibility = View.VISIBLE
            rvOrderDetailsPayment.visibility = View.VISIBLE
            rvOrderDetailsConfirm.visibility = View.GONE
        } else if (orderDetails.order_status == "1" && orderDetails.pay_status == "1") {
            rvOrderDetailsCancel.visibility = View.VISIBLE
            rvOrderDetailsPayment.visibility = View.GONE
            rvOrderDetailsConfirm.visibility = View.GONE
        } else if (orderDetails.order_status == "2" && orderDetails.pay_status == "1") {
            rvOrderDetailsCancel.visibility = View.GONE
            rvOrderDetailsPayment.visibility = View.GONE
            rvOrderDetailsConfirm.visibility = View.VISIBLE
        } else {
            llOrderDetailsStatus.visibility = View.GONE
        }

        rvOrderDetailsGoodsBody.adapter = mGoodsAdapter.apply {
            setNewInstance(orderDetails.order_goods)
        }

        tvOrderDetailsGoodsNum.text = "${orderDetails.total_goods_num}件"
        tvOrderDetailsTotalMoney.text = "￥${orderDetails.total_amount}"
        tvOrderDetailsShippingPrice.text = "￥${orderDetails.shipping_price}"
        tvOrderDetailsCouponPrice.text = "￥${orderDetails.coupon_price}"
        tvOrderDetailsIntegralPrice.text = "￥${orderDetails.integral_price}"
        tvOrderDetailsPayMoney.text = "￥${orderDetails.order_amount}"

        tvOrderDetailsAddressP.text = "${orderDetails.consignee}\t\t${orderDetails.mobile}"
        tvOrderDetailsAddress.text = orderDetails.full_address
        tvOrderDetailsBeizhu.text = orderDetails.user_note

        tvOrderDetailsSn.text = orderDetails.order_sn
        tvOrderDetailsXiaodanTime.text = orderDetails.createtime
        tvOrderDetailsZhifuTime.text = orderDetails.pay_time

    }

}